package com.mall.struts.action;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.mall.ibatis.bean.Method;
import com.mall.ibatis.bean.ReturnMessage;
import com.mall.service.FunctionService;
import com.mall.service.MethodService;

public class MethodAction extends Action{
	private static final MethodService methodService =  MethodService.getInstance();
	private static final FunctionService functionService = FunctionService.getInstance();
	
	@Override
	public ActionForward execute(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response) throws Exception{

		String type = request.getParameter("type");
		if("loading".equals(type)){
			return loading(mapping,form,request,response);
			
		}else if("list".equals(type)){
			return list(mapping,form,request,response);
			
		}else if("addMethod".equals(type)){
			return addMethod(mapping,form,request,response);
			
		}else if("updateMethod".equals(type)){
			return updateMethod(mapping,form,request,response);
			
		}else if("deleteMethod".equals(type)){
			return deleteMethod(mapping,form,request,response);
			
		}else if("searchUserList".equals(type)){
			return searchUserList(mapping,form,request,response);
			
		}else if("getMethodId".equals(type)){
			return getMethodId(mapping,form,request,response);
			
		}
		return null;
	}
	
	//获取放法的Id号
	private ActionForward getMethodId(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception{
		String xmlStr = methodService.getMethodId();
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.print(xmlStr);
		out.flush();
		out.close();
		return null;
	}

	//方法的Id查找有哪些用户
	private ActionForward searchUserList(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,HttpServletResponse response) throws Exception{
		String methodId = request.getParameter("methodId");
		
		String xml = methodService.selectUserList(methodId).toXML();

		response.setContentType("text/xml;charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.print(xml);
		out.flush();
		out.close();
		return null;
	}
	//方法的删除
	private ActionForward deleteMethod(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response)throws Exception{
		String id = request.getParameter("id");
		ReturnMessage message = methodService.deleteMethod(id);
		String xml = message.toXMLString();
		response.setContentType("text/xml;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.print(xml);
		out.flush();
		out.close();
		return null;
	}
	
	//方法的更新
	private ActionForward updateMethod(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response)throws Exception{
		String id = request.getParameter("id");
		String num = request.getParameter("num");
		String name = request.getParameter("name");
		
		Method method = new Method();
		method.setId(id);
		method.setNum(num);
		method.setName(name);
		
		ReturnMessage message = methodService.updateMethod(method);
		String xml = message.toXMLString();
		response.setContentType("text/xml;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.print(xml);
		out.flush();
		out.close();
		return null;
	}
	
	//方法的添加
	private ActionForward addMethod(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response) throws Exception{
		String num = request.getParameter("num");
		String name = request.getParameter("name");
		
		Method method = new Method();
		method.setNum(num);
		method.setName(name);
		
		ReturnMessage message = methodService.addMethod(method);
		String xml = message.toXMLString();
		response.setContentType("text/xml;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.print(xml);
		out.flush();
		out.close();
		return null;
	}
	//列表的显示
	private ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception{
		String xml = methodService.selectAllMethod().toXML();

		response.setContentType("text/xml;charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.print(xml);
		out.flush();
		out.close();
		return null;
	}
	//页面的加载
	private ActionForward loading(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception{
		functionService.methodFilter(request,response);
		return mapping.findForward("loading");
	}
}
